package com.zxsc.zxmusic.adapter;

import java.util.List;

import com.zxsc.zxmusic.R;
import com.zxsc.zxmusic.manager.SongManager;
import com.zxsc.zxmusic.model.SongInfo;
import com.zxsc.zxmusic.model.SongMenuModel;
import com.zxsc.zxmusic.utils.CommonUtils;

import android.R.menu;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class AdapterAlbum extends BaseExpandableListAdapter{

	private List<List<SongMenuModel>> songs;
	private List<SongMenuModel> menus;
	private Context context;
	OnLongClickLister clickLister;
	public AdapterAlbum(Context context,List<SongMenuModel> menus,List<List<SongMenuModel>> songs) {
		this.context=context;
		this.songs=songs;
		this.menus=menus;
	}
	
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return menus.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return songs.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return menus.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return  songs.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder viewHolder=null;
		if(convertView==null){
			viewHolder=new GroupViewHolder();
			convertView=LayoutInflater.from(context).inflate(R.layout.item_songmenu, parent,false);
			viewHolder.arrowsImage=(ImageView) convertView.findViewById(R.id.songmenu_arrow);
			viewHolder.txtSongMenuName=(TextView) convertView.findViewById(R.id.songmenu_name);
			viewHolder.txtSongCount=(TextView) convertView.findViewById(R.id.songmenu_count);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(GroupViewHolder) convertView.getTag();
		}
		final SongMenuModel menuModel=menus.get(groupPosition);
		viewHolder.txtSongMenuName.setText(menuModel.getMenuName());
		
		if(songs.size()<(groupPosition+1)){
			viewHolder.txtSongCount.setText("共收藏"+0+"首");
		}else{
			
			viewHolder.txtSongCount.setText("共收藏"+songs.get(groupPosition).size()+"首");
		}
//		convertView.setOnLongClickListener(new OnLongClickListener() {
//			
//			@Override
//			public boolean onLongClick(View v) {
//				if(clickLister!=null){
//					clickLister.OnLongClick(groupPosition, menuModel);
//				}
//				return false;
//			}
//		});
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder viewHolder=null;
		if(convertView==null){
			viewHolder=new ChildViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_song_list, parent,false);
			viewHolder.mTvName = (TextView) convertView
					.findViewById(R.id.tv_name);
			viewHolder.mTvArtist = (TextView) convertView
					.findViewById(R.id.tv_artist);
			viewHolder.mTvDuration = (TextView) convertView
					.findViewById(R.id.tv_duration);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ChildViewHolder) convertView.getTag();
		}
		SongMenuModel menuModel=songs.get(groupPosition).get(childPosition);
		viewHolder.mTvName.setText(menuModel.getSongName()!=null && menuModel.getMenuName().length()>0?menuModel.getSongName():"未知");
		
		SongInfo info=SongManager.with(context).getSongById(menuModel.getSongId());

		if(info!=null){
			
			viewHolder.mTvArtist
			.setText(info.getArtist() + " - " + info.getAlbum());
			viewHolder.mTvDuration.setText(CommonUtils.durationToString(info
					.getDuration())
					+ " - "
					+ CommonUtils.getFileSize(info.getPath()));
		}
		
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}


	class GroupViewHolder{
		ImageView arrowsImage;
		TextView txtSongMenuName;
		TextView txtSongCount;
	}
	class ChildViewHolder{
			View aboveView;
			View behindView;
			TextView mTvName;
			TextView mTvArtist;
			TextView mTvDuration;
		
	}
	
	public void setOnLongClickLister(OnLongClickLister clickLister){
		this.clickLister=clickLister;
	}
	
	public interface OnLongClickLister{
		public void OnLongClick(int position,SongMenuModel menuModel);
	}



	
}
